<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.java.FamTree.*"%>

<% 
	Connection con = null;
	ArrayList<User> users = new ArrayList<User>();
	try {
	    con = DriverManager.getConnection("jdbc:mysql://webdev.cs.uwosh.edu:4381/severa24", "severa24", "d450224");
  
	    /*
	    * Grab all users that are not admins.
	    */
	    PreparedStatement getUsers = con.prepareStatement(
	            "SELECT UserId, Username FROM User WHERE IsAdmin=0");
	
	    ResultSet userSet = getUsers.executeQuery();
	    while(userSet.next()) {
	        User user = new User();
	        user.UserId = userSet.getInt(1);
	        user.Username = userSet.getString(2);
	        users.add(user);
	    }               
	} catch (SQLException ex) { 
	    System.err.println(ex); 
	} finally {
	    if (con != null ) {
	        try {
	            con.close();
	        } catch (SQLException ex) {
	            System.err.println(ex);
	        }
	    }
	}
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<Div style="text-align:right;background-color:#F0F0F0;" id="TopMenuBar" >
		<%
			out.print("<span id='WelcomSpan'>Welcome " + session.getAttribute("Username") + "  </span>");
			out.print("<a href='index.jsp'><button>Logout</button></a>");
			if((Boolean)session.getAttribute("IsAdmin")){
				out.print("<a href='Admin.jsp'><button>Admin</button></a>");
			}
		%>
	</Div>
	<link rel="stylesheet" type="text/css" href="Scripts/DataTables-1.9.4/media/css/jquery.dataTables.css">
 	<link rel="stylesheet" type="text/css" href="Scripts/DataTables-1.9.4/media/css/demo_table.css">
	<script type="text/javascript" src="Scripts/jquery-1.8.3.js"></script>
	<script type="text/javascript" src="Scripts/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
	<script type="text/javascript">
		$(document).ready(function () {
			$('#UserTable').dataTable();
		});
	</script>
	<div>
	<br/>
	<br/>
	<br/>
	<br/>
		<div style="width:500px; margin:0 auto;">
			<table id="UserTable" class="display" align="center">
				<thead>
					<tr>
						<th>Username:</th>
						<th>Id:</th>
						<th>Edit:</th>
					</tr>
				</thead>
				<tbody>
					<% 
						for(User u : users){
							out.print("<tr><td>"+ u.Username + "</td><td>" + u.UserId + "</td><td><a href='EditUserPermission.jsp?UserId=" + u.UserId + "'><button>Edit</button></a></td></tr>");
						}
					%>
				</tbody>
			</table>
		</div>
	</div>
</body>
</html>